package com.rockcent.mall.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.mall.Routes;
import com.rockcent.mall.lib.dto.MarketMerchantCooperationMessageDto;
import com.rockcent.mall.lib.domain.MarketMerchantCooperationMessage;
import com.rockcent.mall.lib.dto.QueryPagingDto;
import com.rockcent.mall.lib.dto.UserInfoDto;
import com.rockcent.mall.lib.helper.BaseUserHelper;
import com.rockcent.mall.lib.repository.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by HK on 16/9/14.
 */
@RestController
public class MarketMerchantCooperationMessageController extends BaseController {

    private final Logger log = LoggerFactory.getLogger(MarketMerchantCooperationMessageController.class);

    @Autowired
    private BaseUserHelper userHelper;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private MarketMerchantCooperationRepository marketMerchantCooperationRepository;
    @Autowired
    private MarketMerchantCooperationRecordRepository marketMerchantCooperationRecordRepository;
    @Autowired
    private MarketMerchantCooperationMessageRepository marketMerchantCooperationMessageRepository;
    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private MallRepository mallRepository;
    @Autowired
    private MerchantRepository merchantRepository;

    /**
     * @api {get} /market/merchant/cooperation/message_page 商家合作管理 分销消息 分页
     * @apiGroup market_merchant_cooperation_message
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain 请求域名
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     *      "code": "1",
     *      "desc": "成功",
     *      "data": {
     *          "data": [
     *              {
     *                  "id": 1,
     *                  "dateCreated": "2016-09-12 19:17:34",
     *                  "lastUpdated": "2016-09-12 19:17:34",
     *                  "version": 1,
     *                  "title": "审核记录标题",
     *                  "content": "审核记录内容",
     *                  "merchantId": 1
     *              }
     *          ],
     *          "totalCount": 1,
     *          "totalPage": 1,
     *          "pageNo": 0,
     *          "pageSize": 5,
     *          "url": "/merchant/cooperation/message_page",
     *          "prevUrl": "/market/merchant/cooperation/message_page?pageNo=0&pageSize=5",
     *          "nextUrl": "/market/merchant/cooperation/message_page?pageNo=0&pageSize=5"
     *     }
     * }
     *
     */
    @RequestMapping(value = Routes.MARKET_MERCHANT_COOPERATION_MESSAGE_PAGE, method = RequestMethod.GET)
    public JsonResult<PageResult<MarketMerchantCooperationMessage>> messagePage(QueryPagingDto queryPagingDto, HttpServletRequest request) {
        UserInfoDto userInfoDto = userHelper.getSession(request);
        RestDoing<PageResult<MarketMerchantCooperationMessage>> doing = jsonResult -> {
            Page<MarketMerchantCooperationMessage> page = marketMerchantCooperationMessageRepository.findByMerchantIdAndIsDeleteOrderByDateCreatedDesc(userInfoDto.merchant.getId(), Whether.NO, this.getPageRequest(queryPagingDto.getPageNo(), 5));
            PageResult<MarketMerchantCooperationMessage> messagePageResult = new PageResult<>(page.getNumber(), page.getSize(), page.getTotalElements(), page.getContent(), Routes.MARKET_MERCHANT_COOPERATION_MESSAGE_PAGE);
            jsonResult.data = messagePageResult;
        };
        return doing.go(userInfoDto, request, objectMapper, log);
    }


    /**
     * @api {put} /market/merchant/cooperation/message_delete 商家合作管理 分销消息 删除
     * @apiGroup market_merchant_cooperation_message
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain 请求域名
     *
     * @apiParam {Long} id 消息id
     * @apiSuccessExample {json} Success-Response:
     * {
     *      "code": "1",
     *      "desc": "成功"
     * }
     *
     */
    @RequestMapping(value = Routes.MARKET_MERCHANT_COOPERATION_MESSAGE_DELETE, method = RequestMethod.PUT)
    public JsonResult messageDelete(@RequestBody MarketMerchantCooperationMessageDto dto, HttpServletRequest request) {
        UserInfoDto userInfoDto = userHelper.getSession(request);
        RestDoing<PageResult<MarketMerchantCooperationMessage>> doing = jsonResult -> {
            if (dto.getId() == null) {
                jsonResult.errorParam(MarketMerchantCooperationMessage.CHECK_ID_IS_NOT_NULL,log);;
                return;
            }
            MarketMerchantCooperationMessage marketMerchantCooperationMessage = marketMerchantCooperationMessageRepository.findByIdAndMerchantId(dto.getId(), userInfoDto.merchant.getId());
            if (marketMerchantCooperationMessage == null) {
                jsonResult.remind(MarketMerchantCooperationMessage.REMIND_RECORD_IS_NOT_EXIST,log);;
                return;
            }
            marketMerchantCooperationMessage.setIsDelete(Whether.YES);
            marketMerchantCooperationMessageRepository.save(marketMerchantCooperationMessage);
        };
        return doing.go(userInfoDto, request, objectMapper, log);
    }


}
